<?php


namespace App\Http\Controllers;


use App\Server\AliOSS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ShopController extends Controller
{
    /**
     * 渔具店列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function index(Request $request)
    {
        $lat = $request->lat;
        $lng = $request->lng;
        if (!$lat) {
            $location = default_location();
            $lat = $location['lat'];
            $lng = $location['lng'];
        }

        $model = DB::table('shops')->where('status', 1)->select(['id', 'name', 'img', 'address', 'phone', 'map_lat', 'map_lng'])->whereNull('deleted_at');
        $model->addSelect(\DB::raw("acos(cos(" . $lat . "*pi()/180)*cos(map_lat*pi()/180)*cos(" . $lng . "*pi()/180-map_lng*pi()/180)+sin(" . $lat . "*pi()/180)*sin(map_lat * pi()/180)) * 6367000 AS distance"))->orderBy('distance','ASC');
        $data = $model->paginate(10);
        foreach ($data as &$item) {
            $item->img = img_url(AliOSS::getBannerName($item->img));
            $item->distance = distance_text($item->distance, $request->lat);
        }
        return $this->success($data);
    }
}